// //index.js
// //获取应用实例
const app = getApp()
Page({
  data:{
    nowTemp: '',
    nowWeather: '',
    dressIndex: '',
    exerciseIndex: '',
    washIndex: '',
    travelIndex: '',
    todayData: '',
    todayTempScope: '',
    weatherImage: '',
  },
  /**
 * 生命周期函数--监听页面加载
 */
onLoad: function(options){
  wx.request({
    url: 'https://v.juhe.cn/weather/index',
    data:{
      format: 2,
      cityname: '成都',
      key: '2c8179b21bc6c65c4597acf002f12106'
    },
    success(res){
      this.getNow(res)
      this.getIndex(res)
    }
  })
},
/**
 * 生命周期函数-监听事件加载
 */
// onLoad: function(options){
//   this.getWeatherData() = {

//   }
// },
getWeatherData(callback){
  wx.request({
    url: 'https://v.juhe.cn/weather/index',
    data:{
      format: 2,
      cityname: '成都',
      key: '2c8179b21bc6c65c4597acf002f12106'
    },
    success(res){
      this.getNow(res)
      this.getIndex(res)
    },
    // complete(){
    //   callback && callback()
    // }
  })
},
  getNow(res){
    let temp = res.data.result.sk.temp
    let weather = res.data.result.today.weather
    this.setData({
      nowTemp: temp,
      nowWeather: weather
    })
    var imageName = ''
    let endIndex = weather.indexOf('转')
    if(endIndex>=0){
      imageName = weather.setString(0,endIndex)
    }else{
      imageName = weather
    }
    wx.setNavigationBarColor({
      backgroundColor: '#ccccc',
      frontColor: weatherColorMap(weatherMap(imageName)),
    })
    this.setData({
      weatherImage: weatherMap(imageName)
    })
  },
  getIndex(res){
    let dress = res.data.result.today.dressing_index
    let exercise = res.data.result.today.exercise_index
    let wash = res.data.result.today.wash_index
    let travel = res.data.result.today.travel_index
    this.setData({
      dressIndex: dress,
      exerciseIndex: exercise,
      washIndex: wash,
      travelIndex: travel,
    })
  },
  getToday(res){
    let data = res.data.result.today.data_y
    let tempScope = res.data.result.today.temperature
    this.setData({
      todayTempScope: data,
      weatherImage: tempScope
    })
  },
  onPullDownRefresh: function(){
    this.getWeatherData(
      wx.stopPullDownRefresh()
    )
  }
})
